import 'package:animated_bottom_navigation_bar/animated_bottom_navigation_bar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:nvshortvideo/nvshortvideo.dart';
import 'package:vischat/core/annotations/getx_route_enhanced.dart';
import 'package:vischat/generated/icons/app_icons.dart';
import 'package:vischat/modules/home/home_binding.dart';
import 'package:vischat/modules/home/home_controller.dart';
import 'package:vischat/modules/home/posts/posts_page.dart';
import 'package:vischat/modules/home/socialize/socialize_page.dart';
import 'package:vischat/modules/home/user/user_page.dart';

@GetXRoute('/home', binding: HomeBinding)
class HomePage extends GetView<HomeController> {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => controller.handleWillPop(context),
      child: Scaffold(
        body: Obx(
          () => IndexedStack(
            index: controller.activeIndex,
            children: [SocializePage(), PostsPage(), UserPage()],
          ),
        ),
        bottomNavigationBar: Obx(
          () => AnimatedBottomNavigationBar.builder(
            itemCount: controller.bottomNavigationMenus.length,
            tabBuilder:
                (index, isActive) => _buildTabContent(index, isActive, 48.r),
            activeIndex: controller.activeIndex,
            onTap: controller.onItemSelected,
            gapLocation: GapLocation.none,
          ),
        ),
      ),
    );
  }

  Widget _buildTabContent(int index, bool isActive, double iconSize) {
    switch (index) {
      case 0:
        return Center(
          child: Obx(() => Badge(
            label: Text('${controller.unread}'),
            isLabelVisible: controller.unread > 0,
            child: buildIcon(index, isActive, iconSize),
          )),
        );
      case 1:
        return Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              decoration: BoxDecoration(
                color: isActive ? Colors.lightBlueAccent : Colors.white,
                border: Border.all(width: 1.w, color: Colors.black26),
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(4.r),
                  bottomLeft: Radius.circular(4.r),
                ),
              ),
              child: Icon(
                controller.global.isImmerse.value
                    ? AppIcons.arrow_left_fat
                    : AppIcons.arrow_right_fat,
                color: isActive ? Colors.white : Colors.black12,
                size: iconSize,
              ),
            ),
            GestureDetector(
              onTap: () {
                NvVideoConfig videoConfig = NvVideoConfig();
                shortVideoOperator().startVideoCapture(config: videoConfig);
              },
              child: Container(
                decoration: BoxDecoration(
                  border: Border.all(width: 1.w, color: Colors.black26),
                ),
                child: Icon(
                  AppIcons.add,
                  size: iconSize,
                  color: Colors.black26,
                ),
              ),
            ),
          ],
        );
      default:
        return Center(child: buildIcon(index, isActive, iconSize));
    }
  }

  Widget buildIcon(int index, bool isActive, double iconSize) {
    return Icon(
      controller.bottomNavigationMenus[index],
      size: iconSize,
      color: isActive ? Colors.blue : Colors.grey,
    );
  }
}
